package com.zxbg.service.impl.systemtoolsServiceImpl;

import com.zxbg.dao.EmployeeDao;
import com.zxbg.dao.InfoBlockMenuDao;
import com.zxbg.dao.SystemtoolsLeftMenuDao;
import com.zxbg.domain.Employee;
import com.zxbg.domain.InfoBlockMenu;
import com.zxbg.domain.Internationalization;
import com.zxbg.service.systemtoolsService.InfoBlockMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InfoBlockMenuMenuServiceImpl implements InfoBlockMenuService {
    @Autowired
    InfoBlockMenuDao infoBlockMenuDao;
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    SystemtoolsLeftMenuDao systemtoolsLeftMenuDao;
    @Autowired
    OrganizationalStructureServiceImpl organizationalStructureService;

    @Override
    public boolean updateInfoBlockMenu(InfoBlockMenu infoBlockMenu, Integer tenant_id) {
        infoBlockMenu.setUpdate_time(new Date());
        InfoBlockMenu oldBlock = infoBlockMenuDao.selectInfoBlockById(infoBlockMenu.getId(), tenant_id);
        Integer rowNumber = infoBlockMenuDao.updateInfoBlockMenu(infoBlockMenu);
        InfoBlockMenu newBlock = infoBlockMenuDao.selectInfoBlockById(infoBlockMenu.getId(), tenant_id);
        if(oldBlock.getMenu_name_zh() != newBlock.getMenu_name_zh()) {
            systemtoolsLeftMenuDao.updateSystemtoolsLeftMenuNameByMenuCode(newBlock.getId(), newBlock.getMenu_name_zh() + "管理", tenant_id);
        }
        return rowNumber > 0;
    }

    @Override
    public List<InfoBlockMenu> getAllInfoBlockMenu(Integer tenant_id) {
        List<InfoBlockMenu> infoBlockMenuList = infoBlockMenuDao.getAllInfoBlock(tenant_id);
        for (int i = 0; i < infoBlockMenuList.size(); i++) {
            InfoBlockMenu infoBlockMenu = infoBlockMenuList.get(i);
            Internationalization internationalization = new Internationalization(infoBlockMenu.getMenu_name_zh(), infoBlockMenu.getMenu_name_en());
            infoBlockMenu.setMenu_name(internationalization);
            Employee employee = employeeDao.getEmployeeByEmployeeId(infoBlockMenu.getUpdater_id(), tenant_id);
            infoBlockMenu.setUpdater(organizationalStructureService.getAllKindsDept(employee));
        }
        return infoBlockMenuList;
    }
}
